package com.breeze.toolkit.retrofit;

import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

/**
 * Created by john on 2016/8/23.
 */
public class HostSelectionInterceptor implements Interceptor {
	private volatile String host;

	public void setHost(String host) {
		this.host = host;
	}

	@Override
	public Response intercept(Chain chain) throws IOException {
		Request request = chain.request();
		if (host != null) {
			HttpUrl newUrl = request.httpUrl().newBuilder()
					.host(host)
					.build();
			request = request.newBuilder()
					.url(newUrl)
					.build();
		}
		return chain.proceed(request);
	}
}
